package com.fmdj.odr.service.impl;

import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.odr.db.dao.OrderBillDao;
import com.fmdj.odr.db.dao.OrderDao;
import com.fmdj.odr.db.dao.OrderProfitsharingDao;
import com.fmdj.odr.db.pojo.OrderProfitsharingEntity;
import com.fmdj.odr.service.OrderBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderBillServiceImpl implements OrderBillService {
    @Autowired
    private OrderBillService  orderBillService;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderProfitsharingDao orderProfitsharingDao;
    @Autowired
    private OrderBillDao orderBillDao;

    @Override
    @Transactional
    @LcnTransaction
    public int updateBillFee(Map map) {
        int rows = orderBillDao.updateBillFee(map);
        if (rows!=1) {
            throw new GlobalException("更新账单详细信息失败");
        }

        rows = orderDao.updateOrderMileageAndFee(map);
        if (rows!=1) {
            throw new GlobalException("更新订单详细信息失败");
        }
        OrderProfitsharingEntity entity = new OrderProfitsharingEntity();
        entity.setOrderId(MapUtil.getLong(map, "orderId"));
        entity.setRuleId(MapUtil.getLong(map, "ruleId"));
        entity.setAmountFee(new BigDecimal(MapUtil.getStr(map, "total")));
        entity.setPaymentFee(new BigDecimal(MapUtil.getStr(map, "paymentFee")));
        entity.setPaymentRate(new BigDecimal(MapUtil.getStr(map, "paymentRate")));
        entity.setTaxFee(new BigDecimal(MapUtil.getStr(map, "taxFee")));
        entity.setTaxRate(new BigDecimal(MapUtil.getStr(map, "taxRate")));
        entity.setSystemIncome(new BigDecimal(MapUtil.getStr(map, "systemIncome")));
        entity.setDriverIncome(new BigDecimal(MapUtil.getStr(map, "driverIncome")));
        rows = orderProfitsharingDao.insert(entity);
        if (rows!=1) {
            throw new GlobalException("插入分账记录失败");
        }
        return rows;
    }

    @Override
    public HashMap searchReviewDriverOrderBill(Map map) {
        HashMap hashMap = orderBillDao.searchReviewDriverOrderBill(map);
        return hashMap;
    }

    @Override
    public int updateBillPayment(Map map) {
        int rows = orderBillDao.updateBillPayment(map);
        if (rows!=1) {
            throw new GlobalException("更新账单实付金额失败");
        }
        return rows;
    }
}
